/*
** Copyright (c) 2015, yinqiang
** All rights reserved.
** 
** Redistribution and use in source and binary forms, with or without modification,
** are permitted provided that the following conditions are met:
** 
** * Redistributions of source code must retain the above copyright notice, this
**   list of conditions and the following disclaimer.
** 
** * Redistributions in binary form must reproduce the above copyright notice, this
**   list of conditions and the following disclaimer in the documentation and/or
**   other materials provided with the distribution.
** 
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
** DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
** ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
** ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "base64/libbase64.h"

#define PROGNAME      "b64"      /* default program name */
#define B64_VERSION   "1.0.0"
#define B64_COPYRIGHT "Copyright (c) 2015, yinqiang"

#define IS(s)   (strcmp(argv[i],s)==0)

static const char* progname = PROGNAME; /* actual program name */

static void usage(const char* message)
{
    if (*message == '-')
        fprintf(stderr, "%s: unrecognized option ' %s '\n", progname, message);
    else
        fprintf(stderr, "%s: %s\n", progname, message);
    fprintf(stderr,
        "usage: %s [options].\n"
        "Available options are:\n"
        "  -e content   encode\n"
        "  -o content   decode\n"
        "  -v       show version information\n",
        progname);
    exit(EXIT_FAILURE);
}

int main(int argc, char* argv[])
{
    int i, version = 0, encoded = 0, decoded = 0;
    char cnt_e[256] = {'\0'}, cnt_d[256] = {'\0'};
    char *cnt_oe, *cnt_od;
    
    if (argv[0] != NULL && *argv[0] != 0)
        progname = argv[0];

    for (i=1; i<argc; i++)
    {
        if (*argv[i] != '-')    /* end of options; keep it */
            break;
        else if (IS("-v"))      /* show version */
        {
            ++version;
            break;
        }
        else if (IS("-d"))      /* decode */
        {
            cnt_od = argv[++i];
            Base64decode(cnt_d, cnt_od);
            decoded = 1;
            break;
        }
        else if (IS("-e"))     /* encode */
        {
            cnt_oe = argv[++i];
            Base64encode(cnt_e, cnt_oe, strlen(cnt_oe));
            encoded = 1;
            break;
        }
        else                    /* unknown option */
            usage(argv[i]);
    }
    if (version)
    {
        fprintf(stdout, "%s  %s\n", B64_VERSION, B64_COPYRIGHT);
        if (version == argc-1)
            exit(EXIT_SUCCESS);
    }
    if (encoded)
    {
        fprintf(stdout, "%s\n", cnt_e);
        exit(EXIT_SUCCESS);
    }
    if (decoded)
    {
        fprintf(stdout, "%s\n", cnt_d);
        exit(EXIT_SUCCESS);
    }

    return EXIT_SUCCESS;
}